<?php
    $dummy_json = ' {
       "project_name":"wpcreative",
       "revision_count":1,
       "repository_path":"https://wpcreative.googlecode.com/svn/",
       "revisions":[
          {
             "url":"http://wpcreative.googlecode.com/svn-history/r106/",
             "path_count":1,
             "modified":[
                "/trunk/Projects/2012.12/#1_lhablog/template/style.css"
             ],
             "added":[
    
             ],
             "message":"[No log message]",
             "revision":106,
             "timestamp":1354698035,
             "removed":[
    
             ],
             "author":"lehoaian.it@gmail.com"
          }
       ]
    }';
    
    // $dummy_obj = 's:390:"{"project_name":"wpcreative","revision_count":1,"repository_path":"https://wpcreative.googlecode.com/svn/","revisions":[{"url":"http://wpcreative.googlecode.com/svn-history/r113/","path_count":1,"modified":["/trunk/Projects/2012.12/#1_lhablog/template/style.css"],"added":[],"message":"[No log message]","revision":113,"timestamp":1354706577,"removed":[],"author":"lehoaian.it@gmail.com"}]}";';
    
    
    define(ROOT, '/home5/lehoaian/public_html/');
    $revision_data = json_decode(file_get_contents('php://input'));
    //$revision_data = json_decode($dummy_json);
    
    //print_r($revision_data);
    if($revision_data == '') return;
    
    
    $pair_svn_web = array(
        // '/trunk/Projects/2012.12/#1_lhablog' => 'lhablog/wp-content/themes',
        '/trunk/Projects/2012.12/#2_depl_tool' => 'depl',
        '/trunk/Projects/2012.12/#1_lhablog/atlas_theme' => 'lhablog/wp-content/themes/atlas'
    );
    
    // Get project target folder.
    
    $files = $revision_data->revisions[0]->added;
    // error_log('Added file' . sprintf($files));
    
    if(count($files) > 0) {
        foreach($files as $file){
            // Only process file
            if(preg_match('\'(?<!/)/(\\w+\\.\\w+)?$\'', strtolower($file)) !== FALSE) {
                $changes[] = $file;
            }
        }
    }
    
    $files = $revision_data->revisions[0]->modified;
    // error_log('Modified file '. sprintf($files));
    
    if(count($files) > 0) {
        foreach($files as $file){
            // Only process file
            if(preg_match('\'(?<!/)/(\\w+\\.\\w+)?$\'', strtolower($file)) !== FALSE) {
                $changes[] = $file;
            }
        }
    }
    // print_r($changes);
    error_log('------------------' . $revision_data->revisions[0]->message . '------------------');
    //error_log('pair_svn_web : ' . $key);
    error_log('changes : ' . count($changes) . 'files');
    
    //get file
    $remote_file = "";
    $local_file = "";
    
    if($changes) {
        $file = '';
        foreach($changes as $file) {
            if(is_string($file)) {
                // Remote file
                $remote_file = str_replace('https','http', str_replace('#', urlencode('#'), $revision_data->repository_path . $file));
                
                $need_depl = false;
                // Setting local save file
                foreach(array_keys($pair_svn_web) as $key) {
                    // error_log('pair_svn_web' . $key);
                    // error_log('pair_svn_web' . $pair_svn_web[$key]);
                    if(strpos($file, $key) !== FALSE) {
                        $local_file = ROOT. '/' . str_replace($key, $pair_svn_web[$key], $file );
                        $local_file = str_replace('//', '/', $local_file );
                        $need_depl = true;
                    }
                }
                
                if(!$need_depl) return; 
                
                // error_log($remote_file);
                // error_log($local_file);
                error_log('Remote file :' . $remote_file);
                error_log('Local file :' . $local_file);
                $remote_file_string = '';
                $remote_file_string = file_get_contents($remote_file);
                if($remote_file_string != ''){
                    // Remove double slashes
                    
                    
                    //Get folder
                    $local_folder = substr($local_file, 0, strripos($local_file, '/') + 1);
                    
                    if (!is_dir($local_folder)) {
                        mkdir($local_folder);
                    }
                    
                    // error_log('local_folder' . $local_folder);
                    
                    // file_put_contents("data_log.txt",$remote_file_string);
                    
                    if($fhandler = fopen($local_file, 'w')) {
                        fwrite($fhandler, $remote_file_string);
                        fclose($fhandler);
                    } else {
                        error_log('Fopen Bug');
                    }
                }
            } else {
                error_log('Unknown Bug');
                
            }
        }
    }
    error_log('------- END COMMIT ' . $revision_data->revisions[0]->timestamp . '------------');
    
    file_put_contents("log.txt",$revision_data);
    /*SVN*/
?>